package academic.concordia.bankofquebec.managedbean;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;

import org.springframework.context.annotation.Scope;
import org.springframework.security.access.prepost.PreAuthorize;

import academic.concordia.bankofquebec.bean.bankAccount.IBankAccountServices;
import academic.concordia.bankofquebec.bean.session.SessionData;
import academic.concordia.bankofquebec.entity.CheckingsAccount;
import academic.concordia.bankofquebec.entity.SavingsAccount;

@ManagedBean(name="checkingsSavingsAccountOpeningManagedBean")
@Scope("session")
public class CheckingsSavingsAccountOpeningManagedBean implements Serializable{

	private static final long serialVersionUID = 1L;
	
	private CheckingsAccount checkingsAccount;
	
	private SavingsAccount savingsAccount;
	
	@ManagedProperty(value="#{SessionData}")
	private SessionData sessionData;
	
	@ManagedProperty(value="#{BankAccountBean}")
	private IBankAccountServices bankAccountBean;

	@PostConstruct
	void initialize(){
		this.checkingsAccount = new CheckingsAccount();
		this.savingsAccount = new SavingsAccount();
		System.out.println("CheckingSavingsAccountOpeningManagedBean initiating");
	}
	
	public String openAccount(){
		System.out.println("Checkings savings:" + sessionData + " " + sessionData.getClient().getIdentificationType());
		if (!bankAccountBean.addAccount(checkingsAccount, sessionData.getClient())){
			FacesContext facesContext = FacesContext.getCurrentInstance();
			FacesMessage facesMessage = new FacesMessage("Client already have this product");
			facesContext.addMessage(null, facesMessage);
			return "";
		}
		bankAccountBean.addAccount(savingsAccount, sessionData.getClient());
		
		return "products";
	}
	
	public SavingsAccount getSavingsAccount() {
		return savingsAccount;
	}

	public void setSavingsAccount(SavingsAccount savingsAccount) {
		this.savingsAccount = savingsAccount;
	}

	public CheckingsAccount getCheckingsAccount() {
		return checkingsAccount;
	}

	public void setCheckingsAccount(CheckingsAccount checkingsAccount) {
		this.checkingsAccount = checkingsAccount;
	}

	public SessionData getSessionData() {
		return sessionData;
	}

	public void setSessionData(SessionData sessionData) {
		this.sessionData = sessionData;
	}

	public IBankAccountServices getBankAccountBean() {
		return bankAccountBean;
	}

	public void setBankAccountBean(IBankAccountServices bankAccountBean) {
		this.bankAccountBean = bankAccountBean;
	}

}
